var subsetsWithDup = function(nums) {
  const ans = [], path = [];
  nums.sort((a, b) => a - b);
  backtracking(0, nums, ans, path); 
  return ans;


};

function backtracking(startIndex, nums, ans, path) {
  ans.push([...path]);

  for (let i = startIndex; i < nums.length; i++) {
    // 同层重复
    if (i > startIndex && nums[i] === nums[i - 1]) continue;
    path.push(nums[i]);
    backtracking(i + 1, nums, ans, path);
    path.pop();
  }
}

const nums = [1, 2, 2]
console.log(subsetsWithDup(nums))